package demo;

//打印10次ABC
public class Test8 {
    public static void main(String[] args) {
        Object locker1 = new Object();
        Object locker2 = new Object();
        Object locker3 = new Object();

        Thread thread1 = new Thread(()->{
            for (int i = 0; i < 10; i++) {

                    try {
                        synchronized (locker1){
                            locker1.wait();
                        }
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }

                    System.out.print("A");
                    synchronized (locker1){
                        locker2.notify();
                    }
            }
        });
        Thread thread2 = new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    synchronized (locker2){
                        locker2.wait();
                    }
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                    System.out.print("B");
                    synchronized (locker2){
                        locker3.notify();
                    }
                }


        });
        Thread thread3 = new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    synchronized (locker3){
                        locker3.wait();
                    }
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                    System.out.println("C");
                    synchronized(locker1){
                        locker1.notify();
                    }
                }

        });
        thread1.start();
        thread2.start();
        thread3.start();

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        synchronized (locker1){
            locker1.notify();
        }

    }

}
